package com.penghaisoft.microwms.product.web.controller.insidewarehouse;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.ProStorageDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.wms.model.ProStorageSn;
import com.penghaisoft.microwms.common.wms.model.RawLockLog;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.product.service.IProStorageSnService;

import java.util.Date;
import java.util.List;

/**
 * @Description 成品库内管理相关Controller
 * @author luoteng
 * @date 2018年6月6日 下午3:02:17 
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/insidewarehouse")
@Slf4j
public class InsideWarehouseController extends BaseController {

	@Autowired
	private IProStorageSnService proStorageSnService;
	/**
	 * @Title storagesnList
	 * @Description 成品库存明细查询
	 * @param page
	 * @param rows
	 * @param proStorageSn
	 * @return
	 */
	@PostMapping("storagesn/list")
	public TransResult storagesnList(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, ProStorageSn proStorageSn) {
		proStorageSn.preQuery();
		Pager<ProStorageSn> result = proStorageSnService.findListByCondition(page, rows, proStorageSn);
		return success(result);
	}
	/**
	 * @Title storagesnList
	 * @Description 成品库存查询
	 * @param page
	 * @param rows
	 * @param proStorageDto
	 * @return
	 */
	@PostMapping("storage/list")
	public TransResult storageList(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, ProStorageDto proStorageDto) {

		Pager<ProStorageDto> result = proStorageSnService.findStorageListByCondition(page, rows, proStorageDto);
		
		String sumInfo = result.getOrder();
		if (sumInfo != null && sumInfo.length() > 0) {
			result.setOrder(sumInfo);
		}
		return success(result);
	}

	/**
	 * @wzhg 18619
	 * @param id
	 * @return
	 */
	@GetMapping("findById/{id}")
	public TransResult findById(@PathVariable("id")String id){
		ProStorageSn proStorageSn = new ProStorageSn();
		if(id != null && !"".equals(id)){
			proStorageSn = proStorageSnService.findById(id);
		}
		return success(proStorageSn);
	}

	/**
	 * 根据id修改合同号(可以置为空可以修改为其他值)
	 * @wzhg 1865
	 * @param rawStorageSn
	 * @return
	 */
	@PostMapping("updateById")
	public TransResult updateById(RawStorageSn rawStorageSn , ProStorageSn proStorageSn){
		Date now = new Date();
		proStorageSn.setLastModifiedBy(getLoginName());
		proStorageSn.setGmtModified(now);
		proStorageSnService.update(proStorageSn);
		return success();
	}
	@GetMapping("storagesn/r/detail")
	public TransResult findRawStorageSnByCode(ProStorageSn proStorageSn) {
		proStorageSn.preQuery();
		List<ProStorageSn> list = proStorageSnService.findProStorageSnByCondition(proStorageSn);
		return success(list);
	}
//	@GetMapping("storagesn/r/query")
//	public TransResult queryRawStorageSnByCode(ProStorageSn proStorageSn) {
//		proStorageSn.preQuery();
//		List<ProStorageSn> list = proStorageSnService.queryProStorageSnByCondition(proStorageSn);
//		return success(list);
//	}

	/**
	 * 修改冻结查询出的结果
	 * @param proStorageSn
	 * @return
	 */
	@GetMapping("storagesn/r/query")
	public TransResult queryRawStorageSnByCode1(ProStorageSn proStorageSn) {
		proStorageSn.preQuery();
		List<ProStorageSn> list = proStorageSnService.queryProStorageSnByCondition1(proStorageSn);
		return success(list);
	}
	/**
	 * 成品冻结解冻
	 * @param list
	 * @return
	 */
	@PostMapping("storagesn/lockAndunlock")
	public TransResult updateList(@RequestBody List<ProStorageSn> list) {
		TransResult reuslt = new TransResult();
		String currentUserName = getLoginName();
		RawLockLog rawLockLog = new RawLockLog();
		rawLockLog.setLockBy(currentUserName);
		Resp resp = new Resp();
		try {
		 resp = proStorageSnService.updateProStorageSnStatus(list,rawLockLog);
		} catch(RuntimeException e){
			reuslt.setCode("0");
			System.out.print(e.getMessage());
			reuslt.setMsg(e.getMessage());
			return reuslt;
		}
		return fromResp(resp);
	}

}
